package com.danco.training.zaneuskaya.faces;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.danco.training.zaneuskaya.domain.Note;
import com.danco.training.zaneuskaya.service.ForumService;
import com.danco.training.zaneuskaya.service.NoteService;
import com.danco.training.zaneuskaya.service.UserService;
import com.danco.training.zaneuskaya.service.exception.UserException;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Component
@Scope("request")
public class NoteBean implements Serializable {

	private static final String FORUM_INFO_FACES_REDIRECT_TRUE = "/forum/info?faces-redirect=true";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = Logger.getLogger(NoteBean.class);

	private List<Note> noteList;

	private String noteText;

	@Autowired
	private ForumBean forumBean;

	@Autowired
	private UserBean userBean;

	@Autowired
	private UserService userService;

	@Autowired
	private NoteService noteService;

	@Autowired
	private ForumService forumService;

	public ForumBean getForumBean() {
		return forumBean;
	}

	public void setForumBean(ForumBean forumBean) {
		this.forumBean = forumBean;
	}

	public String getNoteText() {
		return noteText;
	}

	public void setNoteText(String noteText) {
		this.noteText = noteText;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public List<Note> getNoteList() {

		return noteList;
	}

	public void setNoteList(List<Note> noteList) {
		this.noteList = noteList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public NoteService getNoteService() {
		return noteService;
	}

	public void setNoteService(NoteService noteService) {
		this.noteService = noteService;
	}

	public ForumService getForumService() {
		return forumService;
	}

	public void setForumService(ForumService forumService) {
		this.forumService = forumService;
	}

	public String postNote() {
		Note note = new Note();
		note.setForum(forumBean.getCurrentForum());
		note.setNoteText(noteText);
		note.setNoteTime(new Date());
		note.setNoteSender(userBean.getSelf());
		try {
			noteService.createEntity(note);
		} catch (UserException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return FORUM_INFO_FACES_REDIRECT_TRUE;
	}

}
